<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1300" destroy-on-close
             @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="商品名称" prop="goods_name">
                        <el-input v-model="form.goods_name" placeholder="请输入商品名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="8">
                    <el-form-item label="商品编码" prop="goods_code">
                        <el-input v-model="form.goods_code" placeholder="请输入商品编码" clearable></el-input>
                    </el-form-item>
                </el-col> -->
                <el-col :span="8">
                    <el-form-item label="商品分类" prop="cate_id">
                        <el-cascader v-model="form.cate_id" :options="cateList" :props="typeProps" clearable style="width: 100%;"></el-cascader>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="商品单位" prop="unit_id">
                        <el-select v-model="form.unit_id" filterable style="width: 100%" placeholder="请选择商品单位">
                            <el-option v-for="item in unitList" :key="item.id" :label="item.unit_name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="仓库" prop="ware_id">
                        <el-select v-model="form.ware_id" filterable style="width: 100%" placeholder="请选择仓库">
                            <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="成本价" prop="cost_price">
                        <el-input v-model="form.cost_price" placeholder="请输入成本价" clearable type="number" @focus="onFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="出厂价" prop="factory_price">
                        <el-input v-model="form.factory_price" placeholder="请输入出厂价" clearable type="number" @focus="onFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="8">
                    <el-form-item label="商品单重" prop="weight">
                        <el-input v-model="form.weight" placeholder="请输入商品单重" clearable type="number" @focus="onFocus($event)"></el-input>
                    </el-form-item>
                </el-col> -->
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="商品规格" prop="goods_spec">
                        <el-input v-model="form.goods_spec" placeholder="请输入商品规格" clearable @focus="onFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="8">
                    <el-form-item label="产品型号" prop="goods_model">
                        <el-input v-model="form.goods_model" placeholder="请输入产品型号" clearable @focus="onFocus($event)"></el-input>
                    </el-form-item>
                </el-col> -->
                <el-col :span="8">
                    <el-form-item label="来源" prop="source">
                        <el-select v-model="form.source" filterable style="width: 100%" placeholder="请选择来源">
                            <el-option label="外采" :value="1" />
                            <el-option label="自产" :value="2" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
			<el-row :gutter="1">
				<el-col :span="8">
					<el-form-item label="品牌" prop="brand_id">
						<el-select v-model="form.brand_id" filterable style="width: 100%" placeholder="请选择">
							<el-option v-for="item in brandList" :key="item.id" :label="item.brand_name" :value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="标签" prop="label_id">
						<el-select v-model="form.label_id" filterable style="width: 100%" placeholder="请选择" multiple>
							<el-option v-for="item in labelList" :key="item.id" :label="item.label_name" :value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
             <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="安全库存上限" prop="stock_up">
                        <el-input v-model="form.stock_up" placeholder="请输入安全库存上限" clearable type="number" @focus="onFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="安全库存下限" prop="stock_down">
                        <el-input v-model="form.stock_down" placeholder="请输入安全库存下限" clearable type="number" @focus="onFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
	             <el-col :span="8">
		             <el-form-item label="是否禁用" prop="system_disable">
			             <el-switch v-model="form.system_disable" :active-value="1" :inactive-value="0"></el-switch>
		             </el-form-item>
	             </el-col>
            </el-row>

			<el-row :gutter="1">
				<el-col :span="12">
					<el-form-item label="商品图片" prop="goods_image">
						<sc-upload height="100" width="100" v-model="form.goods_image"></sc-upload>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="附件" prop="enclosure">
						<sc-upload-file v-model="form.enclosure" :limit="1">
							<el-button type="primary" icon="el-icon-upload">选择文件</el-button>
						</sc-upload-file>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="1">
				<el-col :span="24">
					<el-form-item label="备注" prop="remark">
						<el-input v-model="form.remark" placeholder="请输入备注" clearable type="textarea"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="1">
				<el-col :span="12">
					<el-form-item label="备注1" prop="remark_one">
						<el-input v-model="form.remark_one" placeholder="请输入备注1" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="备注2" prop="remark_two">
						<el-input v-model="form.remark_two" placeholder="请输入备注2" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="1">
				<el-col :span="12">
					<el-form-item label="备注3" prop="remark_three">
						<el-input v-model="form.remark_three" placeholder="请输入备注3" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="备注4" prop="remark_four">
						<el-input v-model="form.remark_four" placeholder="请输入备注4" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增商品',
				edit: '编辑商品',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
				goods_name: "",
				goods_code: "",
				cate_id: "",
				unit_id: "",
				ware_id: "",
				cost_price: "",
				factory_price: "",
				weight: "",
				goods_spec: "",
				goods_model: "",
				source: "",
				stock_up: "",
				stock_down: "",
				goods_image: "",
				remark: "",
				remark_one: "",
				remark_two: "",
				remark_three: "",
				remark_four: "",
				system_disable: 0,
				brand_id:'',
				label_id:[]
			},
			//验证规则
			rules: {
				goods_name: [{ required: true, message: '请输入商品名称' }],
				unit_id: [{ required: true, message: '请选择仓库' }],
				cate_id: [{ required: true, message: '请选择分类' }],
			},
            addTemplate:{},
            cateList:[],
            unitList:[],
            wareList:[],
			typeProps: {
				value: "id",
				label: "cate_name",
				checkStrictly: false
			},
			brandList:[],
			labelList:[]
		}
	},
	mounted() {
        this.getCate()  //分类
        this.getUnit() // 单位
        this.getWare() // 仓库
		this.getBrand()
		this.getLabel()

	},
	methods: {
		/**获取标签*/
		async getLabel(){
			var res = await this.$API.goods.goodsTag.all.get();
			this.labelList = res.data
		},
		/**获取品牌*/
		async getBrand(){
			var res = await this.$API.goods.goodsBrand.all.get();
			this.brandList = res.data
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.goods.goods.edit.post(this.form);
					} else {
						res = await this.$API.goods.goods.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        getPid(data){
            this.form.cate_id = data
        },
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            this.form.goods_name = data.goods_name
            this.form.goods_code = data.goods_code
            let cate = data.cate_id.split(',')
            let a = []
            cate.map(item=>{
                a.push(Number(item))
            })
            this.form.cate_id = a
            this.form.unit_id = data.unit_id
            this.form.ware_id = data.ware_id
            this.form.cost_price = data.cost_price
            this.form.factory_price = data.factory_price
            this.form.weight = data.weight
            this.form.goods_spec = data.goods_spec
            this.form.goods_model = data.goods_model
            this.form.source = data.source
            this.form.stock_up = data.stock_up
            this.form.stock_down = data.stock_down
            this.form.goods_image = data.goods_image
            this.form.remark = data.remark
            this.form.remark_one = data.remark_one
            this.form.remark_two = data.remark_two
            this.form.remark_three = data.remark_three
            this.form.remark_four = data.remark_four
            this.form.system_disable = data.system_disable
			this.form.brand_id = data.brand_id
			this.form.label_id = data.label_id
		},
        // 获取分类
        async getCate(){
            var res = await this.$API.goods.goodsCate.all.get();
            this.cateList = res.data
        },
        // 获取单位
        async getUnit(){
            var res = await this.$API.goods.goodsUnit.all.get();
            this.unitList = res.data
        },
        // 获取仓库
        async getWare(){
            var res = await this.$API.goods.ware.all.get();
            this.wareList = res.data
            res.data.map(item=>{
                if(item.is_default == 1){
                    this.form.ware_id = item.id
                }
            })
        },
        onFocus(event){
            event.target.select();
        },
	}
}
</script>

<style></style>
